<?php
namespace admin\service;

/**--tmpl-use--**/
use herosphp\core\Loader;

/**--tmpl-import--**/

class FriendLinkCateService extends CommonService{
    public function __construct($model)
    {
        parent::__construct($model);
    }
    public function getItem($conditions, $fields, $order)
    {
        $item = parent::getItem($conditions, $fields, $order);
        if(!$item) return NULL;
        /**--tmpl-todo--**/

        /**-- foreignitem --**/
        $parent = parent::getItem($item['parent_id']);

        $item['parent_name'] = $parent['name'];
        $item['parent_description'] = $parent['description'];
        $item['parent_addtime'] = $parent['addtime'];
        $item['parent_parent_id'] = $parent['parent_id'];
        $item['parent_app_id'] = $parent['app_id'];
        $item['parent_manager_id'] = $parent['manager_id'];
        $typemap = array (
            0 => '文字',
            1 => '图片',
        );
        $item['type_text'] = $typemap[$item['type']];
        return $item;
    }

    public function getItems($conditions, $fields, $order, $limit, $group, $having)
    {
        $items = parent::getItems($conditions, $fields, $order, $limit, $group, $having);
        if(!$items) return NULL;
        $parentcondi = array();
        $manager_managercondi = array();

        foreach($items as $k => $v)
        {
            if($v['parent_id'])
            {
                $parentcondi[] = $v['parent_id'];
            }
            if($v['manager_id'])
            {
                $manager_managercondi[] = $v['manager_id'];
            }
        }

        $parents = parent::getItems(array('id' => array( '$in' => $parentcondi)));
        $adminDao = Loader::model('adminManager');
        $manager = $adminDao->getItems(array('id' => array('$in' => $manager_managercondi)));
        $typemap = array (
            0 => '文字',
            1 => '图片',
        );
        foreach ($items as $k => $v)
        {
            foreach($parents as $kk => $vv)
            {
                if($vv['id'] == $v['parent_id'])
                {
                    $items[$k]['parent_name'] = $vv['name'];
                    $items[$k]['parent_description'] = $vv['description'];
                    $items[$k]['parent_addtime'] = $vv['addtime'];
                    $items[$k]['parent_parent_id'] = $vv['parent_id'];
                    $items[$k]['parent_app_id'] = $vv['app_id'];
                    $items[$k]['parent_manager_id'] = $vv['manager_id'];

                }
            }
            foreach($manager as $kk => $vv)
            {
                $items[$k]['manager_name'] = '系统';
                /*if($vv['id'] == $v['manager_id'])
                {
                    $items[$k]['manager_name'] = $vv['name'];
                }*/
            }

            $items[$k]['type_text'] = $typemap[$v['type']];

        }
        return $items;
    }
}

